home *** CD-ROM | disk | FTP | other *** search
/ Skunkware 5 / Skunkware 5.iso / src / Tools / vtcl / doc / TraceVar.3 < prev    next >
Encoding:
Text File  |  1995-07-10  |  14.9 KB  |  369 lines

  1. '\"
  2. '\" Copyright (c) 1989-1993 The Regents of the University of California.
  3. '\" All rights reserved.
  4. '\"
  5. '\" Permission is hereby granted, without written agreement and without
  6. '\" license or royalty fees, to use, copy, modify, and distribute this
  7. '\" documentation for any purpose, provided that the above copyright
  8. '\" notice and the following two paragraphs appear in all copies.
  9. '\"
  10. '\" IN NO EVENT SHALL THE UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY
  11. '\" FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES
  12. '\" ARISING OUT OF THE USE OF THIS DOCUMENTATION, EVEN IF THE UNIVERSITY OF
  13. '\" CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  14. '\"
  15. '\" THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY WARRANTIES,
  16. '\" INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
  17. '\" AND FITNESS FOR A PARTICULAR PURPOSE.  THE SOFTWARE PROVIDED HEREUNDER IS
  18. '\" ON AN "AS IS" BASIS, AND THE UNIVERSITY OF CALIFORNIA HAS NO OBLIGATION TO
  19. '\" PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
  20. '\" 
  21. '\" $Header: /user6/ouster/tcl/man/RCS/TraceVar.3,v 1.14 93/05/03 15:53:18 ouster Exp $ SPRITE (Berkeley)
  22. '\" 
  23. '\"----------------------------------------------------------------------------
  24. '\"    @(#) TraceVar.3 26.1 93/10/22 SCOINC
  25. '\"
  26. '\"     Copyright (C) The Santa Cruz Operation, 1992-1993.
  27. '\"     This Module contains Proprietary Information of
  28. '\"    The Santa Cruz Operation, and should be treated as Confidential.
  29. '\"----------------------------------------------------------------------------
  30. .so ../man.macros
  31. .HS Tcl_TraceVar tclc
  32. .BS
  33. .SH NAME
  34. Tcl_TraceVar, Tcl_TraceVar2, Tcl_UntraceVar, Tcl_UntraceVar2, Tcl_VarTraceInfo, Tcl_VarTraceInfo2 \- monitor accesses to a variable
  35. .SH SYNOPSIS
  36. .nf
  37. \fB#include <tcl.h>\fR
  38. .sp
  39. int
  40. \fBTcl_TraceVar(\fIinterp, varName, flags, proc, clientData\fB)\fR
  41. .sp
  42. int
  43. \fBTcl_TraceVar2(\fIinterp, name1, name2, flags, proc, clientData\fB)\fR
  44. .sp
  45. \fBTcl_UntraceVar(\fIinterp, varName, flags, proc, clientData\fB)\fR
  46. .sp
  47. \fBTcl_UntraceVar2(\fIinterp, name1, name2, flags, proc, clientData\fB)\fR
  48. .sp
  49. ClientData
  50. \fBTcl_VarTraceInfo(\fIinterp, varName, flags, proc, prevClientData\fB)\fR
  51. .sp
  52. ClientData
  53. \fBTcl_VarTraceInfo2(\fIinterp, name1, name2, flags, proc, prevClientData\fB)\fR
  54. .SH ARGUMENTS
  55. .AS Tcl_VarTraceProc prevClientData
  56. .AP Tcl_Interp *interp in
  57. Interpreter containing variable.
  58. .AP char *varName in
  59. Name of variable.  May refer to a scalar variable, to
  60. an array variable with no index, or to an array variable
  61. with a parenthesized index.
  62. .AP int flags in
  63. OR-ed combination of the values TCL_TRACE_READS, TCL_TRACE_WRITES, and
  64. TCL_TRACE_UNSETS, and TCL_GLOBAL_ONLY.  Not all flags are used by all
  65. procedures.  See below for more information.
  66. .AP Tcl_VarTraceProc *proc in
  67. Procedure to invoke whenever one of the traced operations occurs.
  68. .AP ClientData clientData in
  69. Arbitrary one-word value to pass to \fIproc\fR.
  70. .AP char *name1 in
  71. Name of scalar or array variable (without array index).
  72. .AP char *name2 in
  73. For a trace on an element of an array, gives the index of the
  74. element.  For traces on scalar variables or on whole arrays,
  75. is NULL.
  76. .AP ClientData prevClientData in
  77. If non-NULL, gives last value returned by \fBTcl_VarTraceInfo\fR or
  78. \fBTcl_VarTraceInfo2\fR, so this call will return information about
  79. next trace.  If NULL, this call will return information about first
  80. trace.
  81. .BE
  82.  
  83. .SH DESCRIPTION
  84. .PP
  85. \fBTcl_TraceVar\fR allows a C procedure to monitor and control
  86. access to a Tcl variable, so that the C procedure is invoked
  87. whenever the variable is read or written or unset.
  88. If the trace is created successfully then \fBTcl_TraceVar\fR returns
  89. TCL_OK.  If an error occurred (e.g. \fIvarName\fR specifies an element
  90. of an array, but the actual variable isn't an array) then TCL_ERROR
  91. is returned and an error message is left in \fIinterp->result\fR.
  92. .PP
  93. The \fIflags\fR argument to \fBTcl_TraceVar\fR indicates when the
  94. trace procedure is to be invoked and provides information
  95. for setting up the trace.  It consists of an OR-ed combination
  96. of any of the following values:
  97. .TP
  98. \fBTCL_GLOBAL_ONLY\fR
  99. Normally, the variable will be looked up at the current level of
  100. procedure call;  if this bit is set then the variable will be looked
  101. up at global level, ignoring any active procedures.
  102. .TP
  103. \fBTCL_TRACE_READS\fR
  104. Invoke \fIproc\fR whenever an attempt is made to read the variable.
  105. .TP
  106. \fBTCL_TRACE_WRITES\fR
  107. Invoke \fIproc\fR whenever an attempt is made to modify the variable.
  108. .TP
  109. \fBTCL_TRACE_UNSETS\fR
  110. Invoke \fIproc\fR whenever the variable is unset.
  111. A variable may be unset either explicitly by an \fBunset\fR command,
  112. or implicitly when a procedure returns (its local variables are
  113. automatically unset) or when the interpreter is deleted (all
  114. variables are automatically unset).
  115. .PP
  116. Whenever one of the specified operations occurs on the variable,
  117. \fIproc\fR will be invoked.
  118. It should have arguments and result that match the type
  119. \fBTcl_VarTraceProc\fR:
  120. .nf
  121. .RS
  122. typedef char *Tcl_VarTraceProc(
  123. .RS
  124. ClientData \fIclientData\fR,
  125. Tcl_Interp *\fIinterp\fR,
  126. char *\fIname1\fR,
  127. char *\fIname2\fR,
  128. int \fIflags\fR);
  129. .RE
  130. .RE
  131. .fi
  132. The \fIclientData\fP and \fIinterp\fP parameters will
  133. have the same values as those passed to \fBTcl_TraceVar\fR when the
  134. trace was created.
  135. \fIClientData\fR typically points to an application-specific
  136. data structure that describes what to do when \fIproc\fR
  137. is invoked.
  138. \fIName1\fR and \fIname2\fR give the name of the traced variable
  139. in the normal two-part form (see the description of \fBTcl_TraceVar2\fR
  140. below for details).
  141. \fIFlags\fR is an OR-ed combination of bits providing several
  142. pieces of information.
  143. One of the bits TCL_TRACE_READS, TCL_TRACE_WRITES, or TCL_TRACE_UNSETS
  144. will be set in \fIflags\fR to indicate which operation is being performed
  145. on the variable.
  146. The bit TCL_GLOBAL_ONLY will be set whenever the variable being
  147. accessed is a global one not accessible from the current level of
  148. procedure call:  the trace procedure will need to pass this flag
  149. back to variable-related procedures like \fBTcl_GetVar\fR if it
  150. attempts to access the variable.
  151. The bit TCL_TRACE_DESTROYED will be set in \fIflags\fR if the trace is
  152. about to be destroyed;  this information may be useful to \fIproc\fR
  153. so that it can clean up its own internal data structures (see
  154. the section TCL_TRACE_DESTROYED below for more details).
  155. Lastly, the bit TCL_INTERP_DESTROYED will be set if the entire
  156. interpreter is being destroyed.
  157. When this bit is set, \fIproc\fR must be especially careful in
  158. the things it does (see the section TCL_INTERP_DESTROYED below).
  159. The trace procedure's return value should normally be NULL;  see
  160. ERROR RETURNS below for information on other possibilities.
  161. .PP
  162. \fBTcl_UntraceVar\fR may be used to remove a trace.
  163. If the variable specified by \fIinterp\fR, \fIvarName\fR, and \fIflags\fR
  164. has a trace set with \fIflags\fR, \fIproc\fR, and
  165. \fIclientData\fR, then the corresponding trace is removed.
  166. If no such trace exists, then the call to \fBTcl_UntraceVar\fR
  167. has no effect.
  168. The same bits are valid for \fIflags\fR as for calls to \fBTcl_TraceVars\fR.
  169. .PP
  170. \fBTcl_VarTraceInfo\fR may be used to retrieve information about
  171. traces set on a given variable.
  172. The return value from \fBTcl_VarTraceInfo\fR is the \fIclientData\fR
  173. associated with a particular trace.
  174. The trace must be on the variable specified by the \fIinterp\fR,
  175. \fIvarName\fR, and \fIflags\fR arguments (only the TCL_GLOBAL_ONLY
  176. bit from \fIflags\fR is used;  other bits are ignored) and its trace procedure
  177. must the same as the \fIproc\fR argument.
  178. If the \fIprevClientData\fR argument is NULL then the return
  179. value corresponds to the first (most recently created) matching
  180. trace, or NULL if there are no matching traces.
  181. If the \fIprevClientData\fR argument isn't NULL, then it should
  182. be the return value from a previous call to \fBTcl_VarTraceInfo\fR.
  183. In this case, the new return value will correspond to the next
  184. matching trace after the one whose \fIclientData\fR matches
  185. \fIprevClientData\fR, or NULL if no trace matches \fIprevClientData\fR
  186. or if there are no more matching traces after it.
  187. This mechanism makes it possible to step through all of the
  188. traces for a given variable that have the same \fIproc\fR.
  189.  
  190. .SH "TWO-PART NAMES"
  191. .PP
  192. The procedures \fBTcl_TraceVar2\fR, \fBTcl_UntraceVar2\fR, and
  193. \fBTcl_VarTraceInfo2\fR are identical to \fBTcl_TraceVar\fR,
  194. \fBTcl_UntraceVar\fR, and \fBTcl_VarTraceInfo\fR, respectively,
  195. except that the name of the variable has already been
  196. separated by the caller into two parts.
  197. \fIName1\fR gives the name of a scalar variable or array,
  198. and \fIname2\fR gives the name of an element within an
  199. array.
  200. If \fIname2\fR is NULL it means that either the variable is
  201. a scalar or the trace is to be set on the entire array rather
  202. than an individual element (see WHOLE-ARRAY TRACES below for
  203. more information).
  204.  
  205. .SH "ACCESSING VARIABLES DURING TRACES"
  206. .PP
  207. During read and write traces, the
  208. trace procedure can read, write, or unset the traced
  209. variable using \fBTcl_GetVar2\fR, \fBTcl_SetVar2\fR, and
  210. other procedures.
  211. While \fIproc\fR is executing, traces are temporarily disabled
  212. for the variable, so that calls to \fBTcl_GetVar2\fR and
  213. \fBTcl_SetVar2\fR will not cause \fIproc\fR or other trace procedures
  214. to be invoked again.
  215. Disabling only occurs for the variable whose trace procedure
  216. is active;  accesses to other variables will still be traced.
  217. .VS
  218. However, if a variable is unset during a read or write trace then unset
  219. traces will be invoked.
  220. .VE
  221. .PP
  222. During unset traces the variable has already been completely
  223. expunged.
  224. It is possible for the trace procedure to read or write the
  225. variable, but this will be a new version of the variable.
  226. Traces are not disabled during unset traces as they are for
  227. read and write traces, but existing traces have been removed
  228. from the variable before any trace procedures are invoked.
  229. If new traces are set by unset trace procedures, these traces
  230. will be invoked on accesses to the variable by the trace
  231. procedures.
  232.  
  233. .SH "CALLBACK TIMING"
  234. .PP
  235. When read tracing has been specified for a variable, the trace
  236. procedure will be invoked whenever the variable's value is
  237. read.  This includes \fBset\fR Tcl commands, \fB$\fR-notation
  238. in Tcl commands, and invocations of the \fBTcl_GetVar\fR
  239. and \fBTcl_GetVar2\fR procedures.
  240. \fIProc\fR is invoked just before the variable's value is
  241. returned.
  242. It may modify the value of the variable to affect what
  243. is returned by the traced access.
  244. .VS
  245. If it unsets the variable then the access will return an error
  246. just as if the variable never existed.
  247. .VE
  248. .PP
  249. When write tracing has been specified for a variable, the
  250. trace procedure will be invoked whenever the variable's value
  251. is modified.  This includes \fBset\fR commands\fR,
  252. commands that modify variables as side effects (such as
  253. \fBcatch\fR and \fBscan\fR), and calls to the \fBTcl_SetVar\fR
  254. and \fBTcl_SetVar2\fR procedures).
  255. \fIProc\fR will be invoked after the variable's value has been
  256. modified, but before the new value of the variable has been
  257. returned.
  258. It may modify the value of the variable to override the change
  259. and to determine the value actually returned by the traced
  260. access.
  261. .VS
  262. If it deletes the variable then the traced access will return
  263. an empty string.
  264. .VE
  265. .PP
  266. When unset tracing has been specified, the trace procedure
  267. will be invoked whenever the variable is destroyed.
  268. The traces will be called after the variable has been
  269. completely unset.
  270.  
  271. .SH "WHOLE-ARRAY TRACES"
  272. .PP
  273. If a call to \fBTcl_TraceVar\fR or \fBTcl_TraceVar2\fR specifies
  274. the name of an array variable without an index into the array,
  275. then the trace will be set on the array as a whole.
  276. This means that \fIproc\fR will be invoked whenever any
  277. element of the array is accessed in the ways specified by
  278. \fIflags\fR.
  279. When an array is unset, a whole-array trace will be invoked
  280. just once, with \fIname1\fR equal to the name of the array
  281. and \fIname2\fR NULL;  it will not be invoked once for each
  282. element.
  283.  
  284. .SH "MULTIPLE TRACES"
  285. .PP
  286. It is possible for multiple traces to exist on the same variable.
  287. When this happens, all of the trace procedures will be invoked on each
  288. access, in order from most-recently-created to least-recently-created.
  289. When there exist whole-array traces for an array as well as
  290. traces on individual elements, the whole-array traces are invoked
  291. before the individual-element traces.
  292. .VS
  293. If a read or write trace unsets the variable then all of the unset
  294. traces will be invoked but the remainder of the read and write traces
  295. will be skipped.
  296. .VE
  297.  
  298. .SH "ERROR RETURNS"
  299. .PP
  300. Under normal conditions trace procedures should return NULL, indicating
  301. successful completion.
  302. If \fIproc\fR returns a non-NULL value it signifies that an
  303. error occurred.
  304. The return value must be a pointer to a static character string
  305. containing an error message.
  306. If a trace procedure returns an error, no further traces are
  307. invoked for the access and the traced access aborts with the
  308. given message.
  309. Trace procedures can use this facility to make variables
  310. read-only, for example (but note that the value of the variable
  311. will already have been modified before the trace procedure is
  312. called, so the trace procedure will have to restore the correct
  313. value).
  314. .PP
  315. The return value from \fIproc\fR is only used during read and
  316. write tracing.
  317. During unset traces, the return value is ignored and all relevant
  318. trace procedures will always be invoked.
  319.  
  320. .SH "RESTRICTIONS"
  321. .PP
  322. A trace procedure can be called at any time, even when there
  323. is a partically-formed result in the interpreter's result area.  If
  324. the trace procedure does anything that could damage this result (such
  325. as calling \fBTcl_Eval\fR) then it must save the original values of
  326. the interpreter's \fBresult\fR and \fBfreeProc\fR fields and restore
  327. them before it returns.
  328.  
  329. .SH "UNDEFINED VARIABLES"
  330. .PP
  331. It is legal to set a trace on an undefined variable.
  332. The variable will still appear to be undefined until the
  333. first time its value is set.
  334. If an undefined variable is traced and then unset, the unset will fail
  335. with an error (``no such variable''), but the trace
  336. procedure will still be invoked.
  337.  
  338. .SH "TCL_TRACE_DELETED FLAG"
  339. .PP
  340. In an unset callback to \fIproc\fR, the TCL_TRACE_DELETED bit
  341. is set in \fIflags\fR if the trace is being removed as part
  342. of the deletion.
  343. Traces on a variable are always removed whenever the variable
  344. is deleted;  the only time TCL_TRACE_DELETED isn't set is for
  345. a whole-array trace invoked when only a single element of an
  346. array is unset.
  347.  
  348. .SH "TCL_INTERP_DESTROYED"
  349. .PP
  350. When an interpreter is destroyed, unset traces are called for
  351. all of its variables.
  352. The TCL_INTERP_DESTROYED bit will be set in the \fIflags\fR
  353. argument passed to the trace procedures.
  354. Trace procedures must be extremely careful in what they do if
  355. the TCL_INTERP_DESTROYED bit is set.
  356. It is not safe for the procedures to invoke any Tcl procedures
  357. on the interpreter, since its state is partially deleted.
  358. All that trace procedures should do under these circumstances is
  359. to clean up and free their own internal data structures.
  360.  
  361. .SH BUGS
  362. .PP
  363. Tcl doesn't do any error checking to prevent trace procedures
  364. from misusing the interpreter during traces with TCL_INTERP_DESTROYED
  365. set.
  366.  
  367. .SH KEYWORDS
  368. clientData, trace, variable
  369.